<?php

include ("reqvars.php");
$t = stripslashes($t);
$footer="";
$link = <<<EOF
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="fancybox/jquery.easing-1.3.pack.js"></script>
<script type="text/javascript" src="fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.4.js"></script>
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.4.css" media="screen" />
<script type="text/javascript">
$(document).ready(function() {
	$("a#chooseimg").fancybox({
		'hideOnContentClick': false,
		'padding': 10,						
		'width': 750,
		'height': 427,
		'type': 'iframe'
	});
	
	
});
</script>
EOF;
$title = "Choose Image";
$header = <<<EOF
<form action="index.php" method="get">
<input type="text" name="t" id="t" value="{$t}"/>
<select name="tag" id="tag">
<option value="ferrari">ferrari</option>
<option value="dog">dog</option>
<option value="dolphin">dolphin</option>
<option value="beach">beach</option>
<option value="sunset">sunset</option>
<option value="horse">horse</option>
</select>
<input type="submit" />
</form>
EOF;

$t = rawurlencode($t);

$content = "";

$iTer = 0;
$basedir = './paths';
/*
$handle = opendir($basedir);
while ($file = readdir($handle))
{

	if ($file != '.' && $file != '..')
	{
		if (substr($file, -3) == "php")
		{		
			$subcontent = <<<EOF
<span id="imgitem"><a id="chooseimg" href="final.php?t={$t}&n={$iTer}&s=1&u={$u}"><img src="imgtxt.php?t={$t}&n={$iTer}&s=0" id="imgcontent" /></a>
</span>
EOF;
			$content = $content.$subcontent;
			$iTer++;
		}
	}
}
*/
/* The Flickr way */

//get the list of most recent images

$flickrKey = "e2b77b7cbfcf9124617a51ee44dec0db";

$flickrTag = "cat";
$flickrTag = stripslashes(@$_REQUEST['tag']);

if (isset($_REQUEST['category']))
	$flickrTag = @$_REQUEST['category'];
$flickrUrl = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=$flickrKey&tags=$flickrTag&license=commderiv&media=photos&per_page=15&format=rest";
$photos = getFlickrContent($flickrUrl);
foreach ($photos->photo as $photo) {
   	$subcontent = <<<EOF
<span id="imgitem"><a id="chooseimg" href="final.php?t={$t}&f={$photo['farm']}&s={$photo['server']}&i={$photo['id']}&sc={$photo['secret']}"><img src="imgtxt2.php?t={$t}&f={$photo['farm']}&s={$photo['server']}&i={$photo['id']}&sc={$photo['secret']}" id="imgcontent" /></a>
</span>
EOF;
	$content = $content.$subcontent;
}

include ("base.php");
echo $html;


function getFlickrContent($url)
{
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	//some sites reject page requests unless they appear to originate by a person. No problem. The next line is a FreeBSD user running Firefox.
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.2a) Gecko/20021021");
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);

	$ret = curl_exec ($ch);
	curl_close($ch);
	
	$rsp = new SimpleXMLElement($ret);
	return $rsp->photos;
}
?>
